package br.com.statizo.financeiro.view;

import java.awt.Frame;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.DateTickUnitType;
import org.jfree.chart.axis.TickUnits;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.snoghertframework.swt.util.impl.LayoutUtil;
import org.snoghertframework.swt.widgets.impl.SnoghertCombo;
import org.springframework.beans.factory.annotation.Autowired;

import br.com.statizo.financeiro.event.PeriodoEvent;
import br.com.statizo.financeiro.service.CalculoService;
import br.com.statizo.financeiro.service.CalendarService;


public class RelatorioGeralComposite extends Composite{



	private Group groupArea;

	
	private LayoutUtil layoutUtil = new LayoutUtil();


	@Autowired
	private CalendarService calendarService;

	@Autowired
	private CalculoService calculoService;

	private Frame frame = null;
	
	private Composite chartComposite;

	private SnoghertCombo comboMes1;
	private SnoghertCombo comboMes2;
	
	private SnoghertCombo comboAno1;
	private SnoghertCombo comboAno2;
	
	@Autowired
	public RelatorioGeralComposite(Composite initFolder, int style) {

		super(initFolder, style);
	
		if(frame == null){

			setLayout(new FormLayout());
			
			groupArea = new Group(this,SWT.NONE);
			layoutUtil.setFormData(groupArea, 5, 30, 95, 95);
			groupArea.setLayout(new FormLayout());


			//String[] months = calendarService.getMonthNamesForCombo(); 
			//String [] years = calendarService.getYears(); 

			CombosListeners cbListener = new CombosListeners();
//			
//			comboMes1 = new SnoghertCombo(groupArea, "Mês", SWT.NONE);
//			//comboMes1.setValues(months);
//			layoutUtil.setLayoutData(comboMes1, 3, 0, 30, 13);
//			comboMes1.addSelectionListener(cbListener);
//
//			comboAno1 = new SnoghertCombo(groupArea, "Ano", SWT.NONE);
//			//comboAno1.setValues(years);
//			layoutUtil.setLayoutData(comboAno1, 3, 31, 51, 13);
//			comboAno1.addSelectionListener(cbListener);
//			
//			comboMes2 = new SnoghertCombo(groupArea, "Mês", SWT.NONE);
//			//comboMes2.setValues(months);
//			layoutUtil.setLayoutData(comboMes2, 3, 52, 70, 13);
//			comboMes2.addSelectionListener(cbListener);
//			
//			comboAno2 = new SnoghertCombo(groupArea, "Ano", SWT.NONE);
//			//comboAno2.setValues(years);
//			layoutUtil.setLayoutData(comboAno2, 3, 71, 100, 13);
//			comboAno2.addSelectionListener(cbListener);
			
			chartComposite = new Composite(groupArea,SWT.EMBEDDED);
			layoutUtil.setFormData(chartComposite, 15, 0, 100, 100);
			try {
				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			} catch (ClassNotFoundException | InstantiationException
					| IllegalAccessException | UnsupportedLookAndFeelException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
			
			frame = SWT_AWT.new_Frame(chartComposite);
			frame.setVisible(true);

		}else{
			
			frame.removeAll();

		}

		
//		frame.add(getChart());
//		frame.setSize(chartComposite.getBounds().width, chartComposite.getBounds().height);
//		frame.setVisible(true);
	
	
	}



	class CombosListeners extends SelectionAdapter {

//		@Override
//		public void widgetSelected(SelectionEvent e) {
//			createContents();
//		}
		
	}
//	
//	@Override
//	public void createContents() {
//		
//	}
//
//
//	public int getYear(boolean first){
//
//		SnoghertCombo cb = first ? comboAno1 : comboAno2;
//		
//		String strYear = cb.getItem();
//		
//		if(!"".equals(strYear)){
//			return Integer.parseInt(strYear);
//		}else{
//			return calendarService.getYear();
//		}
//		
//	}
//	
//	public int getMonth(boolean first){
//		SnoghertCombo cb = first ? comboMes1 : comboMes2;
//		int month = cb.getValue(); 
//		return month > 0 ? month : calendarService.getRealSelectedMonth();
//	}
//	
//	
//	public ChartPanel getChart(){
//
//		TimeSeriesCollection coll = new TimeSeriesCollection();
//		TimeSeries s1 = new TimeSeries("Despesa");
//		TimeSeries s2 = new TimeSeries("Receita");		
//
//		int firstYear = 2014;//getYear(true);
//		int firstMonth = 1;//getMonth(true);
//		int lastYear = 2014;//getYear(false);
//		int lastMonth = 1;//getMonth(false);
//
//		
//		Month month = new Month(firstMonth,firstYear);
//		while(month.getYearValue() <= lastYear){
//
//			//s1.add(month,calculoService.getDespesas(month.getMonth(), month.getYearValue()));
//			//s2.add(month,calculoService.getReceitas(month.getMonth(), month.getYearValue()));
//
//			if(month.getYearValue() == lastYear && month.getMonth() == lastMonth)
//				break;
//			
//			month = (Month) month.next();
//
//		}
//
//		coll.addSeries(s1);
//		coll.addSeries(s2);
//
//
//		DateFormat f1 = new SimpleDateFormat("MMMM-yyyy");
//		TickUnits monthUnits = new TickUnits();
//
//		monthUnits.add(new DateTickUnit(DateTickUnitType.MONTH, 1, f1 ));
//		monthUnits.add(new DateTickUnit(DateTickUnitType.MONTH, 2, f1 ));
//		monthUnits.add(new DateTickUnit(DateTickUnitType.MONTH, 3, f1 ));
//		monthUnits.add(new DateTickUnit(DateTickUnitType.MONTH, 4, f1 ));
//		monthUnits.add(new DateTickUnit(DateTickUnitType.MONTH, 5, f1 ));
//		monthUnits.add(new DateTickUnit(DateTickUnitType.MONTH, 6, f1 ));
//		monthUnits.add(new DateTickUnit(DateTickUnitType.MONTH, 7, f1 ));
//		monthUnits.add(new DateTickUnit(DateTickUnitType.MONTH, 8, f1 ));
//		monthUnits.add(new DateTickUnit(DateTickUnitType.MONTH, 9, f1 ));
//		monthUnits.add(new DateTickUnit(DateTickUnitType.MONTH, 10, f1 ));
//		monthUnits.add(new DateTickUnit(DateTickUnitType.MONTH, 11, f1 ));
//		monthUnits.add(new DateTickUnit(DateTickUnitType.MONTH, 12, f1 ));
//
//
//
//		JFreeChart jfc = ChartFactory.createTimeSeriesChart("Relatório anual de despesas e receitas", "Meses", "Valor (R$)", coll, true, true, false);
//		XYPlot plot = jfc.getXYPlot();
//		DateAxis axis = (DateAxis) plot.getDomainAxis();
//		//		axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
//		axis.setStandardTickUnits(monthUnits);
//
//		ChartPanel chartPanel = new ChartPanel(jfc);
//		chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
//
//		return chartPanel;
//
//	}
//
//
//
//	@Override
//	public void periodoChanged(PeriodoEvent event) {
//		createContents();
//
//	}
//
//
//
//	@Override
//	public void entitySaved(SaveEvent saveEvent) {
//		createContents();
//	}
//

}

